import axios from "axios";
import router from '../router'
import {curUrl1} from 'util/prodUrl'

export function request(config) {
  const instance = axios.create({
    baseURL: curUrl1,
    timeout: 5000
  })

  instance.interceptors.request.use(res => {
    if (config.url != '/platform/pl/login') {
      res.headers.token = localStorage.getItem("token")
    }
    return res
  }, err => {
    console.log(err);
  })

  instance.interceptors.response.use(res => {
    //console.log(res);
    return res.data
  }, err => {
    console.log(err.response);
    if(err.response.status == 401 || err.response.status == 500){
      localStorage.clear();     //删除用户信息
      //alert('token失效,请重新登录!');
      //要跳转登陆页
      //alert('状态失效')
      
      router.push('/login');
    }
  })

  return instance(config)
}